/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.simulator;

import bobbietalk.gui.dialogs.LocalSimulationWindow;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author roberto
 */
public class Robot2dSimulatorTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        final JFrame f = new JFrame();
        final LocalSimulationWindow simWindow = new LocalSimulationWindow(f, true);

        final JButton openButton = new JButton("Open Simulator");
        final JButton exitButton = new JButton("Exit");

        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {

                simWindow.dispose();
                f.dispose();

            }
        });

        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                simWindow.dispose();
                f.dispose();
                System.exit(0);
            }
        });

        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String program =
                        "   theRobot.leftMotorForward();\n"
                        + " theRobot.rightMotorForward();\n"
                        + "\n"
                        + "  try {\n"
                        + "  Thread.sleep(10*1000); \n"
                        + "  } catch(InterruptedException ex) {\n"
                        + "  }\n"
                        + "        \n"
                        + "\n"
                        + "  theRobot.leftMotorOff();\n"
                        + "  theRobot.rightMotorOff();"
                        ;
                
                simWindow.runCode(program);

                simWindow.setVisible(true);
            }
        });
       

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));

        buttons.add(openButton);
        buttons.add(exitButton);

        f.getContentPane().add(buttons);

        f.pack();

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    f.setVisible(true);
                } catch (Exception ex) {

                    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "can't start bobbietalk", ex);

                    System.exit(1);

                }
            }
        });

    }
}
